package com.chixing.day12.Date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**封装了项目中日期相关的操作
 * 如 日期与字符串的转换,获得日期中的年月日时分秒
 */
public class DateUtils {

    /*
     *字符串转化成日期(LocalDateTime)
     */
     public static LocalDateTime stringToLocalDateTime(String str){
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
         return LocalDateTime.parse(str,formatter);
     }

     /*
      *字符串转化成LocalDate
      */

     public static LocalDate stringTOLocalDate(String str){
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
         return LocalDate.parse(str,formatter);
     }

     /*
      *字符串转化成LocalDate
      */

      public static LocalTime stringToLocalTime(String str ) {
          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
          return LocalTime.parse(str,formatter);
      }
        /*
      * LocalDateTime 转化成字符串
      */
      public static String localDateToString(LocalDate localDate){
          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
          return localDate.format(formatter);
      }

      /*
       *LocalTime 转化成字符串
       *
       */
       public static String localTimeToString(LocalTime localTime){
           DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
           return localTime.format(formatter);
       }

}
